//
//  NovelDatabase.m
//  数据库
//
//  Created by 霍文轩 on 15/8/14.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

// 用于执行数据库语句

#import "DiaryDataBase.h"
#import "DB.h"
#import "Diary.h"

@implementation DiaryDataBase

//+(void)insertWithDiaryid:(NSInteger)diary_id;
//+(void)delegeteWithDiaryid:(NSInteger)diary_id;
//+(NSArray *)selectAll;

+(void)insertWithDiary:(Diary *)diary{
    sqlite3 * db = [DB dbOpen];
    
    NSString *str = [NSString stringWithFormat:
                     @"insert into diary(d_id, d_title, d_pic, d_dayCount, a_image, a_name, d_type, d_address, d_date, d_viewCount) values (%ld, '%@', '%@', '%@', '%@', '%@', %ld, '%@', '%@', '%@') ",
                     diary.diaryid, diary.title, diary.pic, diary.dayCount, diary.authorImage, diary.authorName, diary.type, diary.address, diary.date, diary.viewCount];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    // 判断是否执行成功
    if(result == SQLITE_OK){
        NSLog(@"插入成功 %d",result);
    }else{
        NSLog(@"插入失败 %d",result);
    }
}


+(void)delegeteWithDiaryid:(NSInteger)diary_id{
    sqlite3 *db = [DB dbOpen];
    NSString * str = [NSString stringWithFormat:@"delete from diary where d_id = %ld ",diary_id];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if(result == SQLITE_OK){
        NSLog(@"删除成功 %d",result);
    }else{
        NSLog(@"删除失败 %d",result);
    }
}

+(NSArray *)selectAll{
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select d_id, d_title, d_pic, d_dayCount, a_image, a_name, d_type, d_address, d_date, d_viewCount from diary"];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSInteger d_id = sqlite3_column_int64(stmt, 0); // 参数分别为替身和列
            const unsigned char *d_title = sqlite3_column_text(stmt, 1);
            const unsigned char *d_pic = sqlite3_column_text(stmt, 2);
            const unsigned char *d_dayCount =sqlite3_column_text(stmt, 3);
            const unsigned char *a_image = sqlite3_column_text(stmt, 4);
            const unsigned char *a_name = sqlite3_column_text(stmt, 5);
            int d_type = sqlite3_column_int(stmt, 6);
            const unsigned char *d_address = sqlite3_column_text(stmt, 7);
            const unsigned char *d_date = sqlite3_column_text(stmt, 8);
            const unsigned char * d_viewCount = sqlite3_column_text(stmt, 9);
            
            NSString *diaryTitle = [NSString stringWithUTF8String:(const char *)d_title];
            NSString *diaryPic = [NSString stringWithUTF8String:(const char *)d_pic];
            NSString *diaryDayCount = [NSString stringWithUTF8String:(const char *)d_dayCount];
            NSString *diaryImage = [NSString stringWithUTF8String:(const char *)a_image];
            NSString *diaryName = [NSString stringWithUTF8String:(const char *)a_name];
            NSString *diaryAddress = [NSString stringWithUTF8String:(const char *)d_address];
            NSString *diaryDate = [NSString stringWithUTF8String:(const char *)d_date];
            NSString *diaryViewCount = [NSString stringWithUTF8String:(const char *)d_viewCount];
            
            
            Diary *diary = [Diary diaryWithPic:diaryPic title:diaryTitle date:diaryDate dayCount:diaryDayCount viewCount:diaryViewCount authorImage:diaryImage authorName:diaryName address:diaryAddress type:d_type diaryid:d_id];
            
            
            [array addObject:diary];
        }
    }
    
    // 释放替身
    sqlite3_finalize(stmt);
    
    return array;
}

+ (Diary *)selectWithDiaryid:(NSInteger)diary_id{
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select d_id, d_title, d_pic, d_dayCount, a_image, a_name, d_type, d_address, d_date, d_viewCount from diary where d_id = %ld", diary_id];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            int d_id = sqlite3_column_int(stmt, 0); // 参数分别为替身和列
            const unsigned char *d_title = sqlite3_column_text(stmt, 1);
            const unsigned char *d_pic = sqlite3_column_text(stmt, 2);
            const unsigned char *d_dayCount =sqlite3_column_text(stmt, 3);
            const unsigned char *a_image = sqlite3_column_text(stmt, 4);
            const unsigned char *a_name = sqlite3_column_text(stmt, 5);
            int d_type = sqlite3_column_int(stmt, 6);
            const unsigned char *d_address = sqlite3_column_text(stmt, 7);
            const unsigned char *d_date = sqlite3_column_text(stmt, 8);
            const unsigned char * d_viewCount = sqlite3_column_text(stmt, 9);
            
            NSString *diaryTitle = [NSString stringWithUTF8String:(const char *)d_title];
            NSString *diaryPic = [NSString stringWithUTF8String:(const char *)d_pic];
            NSString *diaryDayCount = [NSString stringWithUTF8String:(const char *)d_dayCount];
            NSString *diaryImage = [NSString stringWithUTF8String:(const char *)a_image];
            NSString *diaryName = [NSString stringWithUTF8String:(const char *)a_name];
            NSString *diaryAddress = [NSString stringWithUTF8String:(const char *)d_address];
            NSString *diaryDate = [NSString stringWithUTF8String:(const char *)d_date];
            NSString *diaryViewCount = [NSString stringWithUTF8String:(const char *)d_viewCount];
            
            
            Diary *diary = [Diary diaryWithPic:diaryPic title:diaryTitle date:diaryDate dayCount:diaryDayCount viewCount:diaryViewCount authorImage:diaryImage authorName:diaryName address:diaryAddress type:d_type diaryid:d_id];
            
            sqlite3_finalize(stmt);
            
            return diary;
        }
    }
    
    // 释放替身
    sqlite3_finalize(stmt);
    
    return nil;
}




@end